package main

import "github.com/hajimehoshi/ebiten/v2"

type Input struct {
	keys map[ebiten.Key]bool
}

func (i *Input) Update(player *Player) {
	i.updateKeys()

	player.playerMoving = false
	player.playerDir = 0
	player.playerUp = false
	player.playerDown = false
	player.playerLeft = false
	player.playerRight = false

	if i.keys[ebiten.KeyW] || i.keys[ebiten.KeyUp] {
		player.playerMoving = true
		player.playerDir = 1
		player.playerUp = true
	}
	if i.keys[ebiten.KeyS] || i.keys[ebiten.KeyDown] {
		player.playerMoving = true
		player.playerDir = 0
		player.playerDown = true
	}
	if i.keys[ebiten.KeyA] || i.keys[ebiten.KeyLeft] {
		player.playerMoving = true
		player.playerDir = 2
		player.playerLeft = true
	}
	if i.keys[ebiten.KeyD] || i.keys[ebiten.KeyRight] {
		player.playerMoving = true
		player.playerDir = 3
		player.playerRight = true
	}
}

func (i *Input) updateKeys() {
	i.keys = make(map[ebiten.Key]bool)

	if ebiten.IsKeyPressed(ebiten.KeyW) || ebiten.IsKeyPressed(ebiten.KeyUp) {
		i.keys[ebiten.KeyW] = true
		i.keys[ebiten.KeyUp] = true
	}
	if ebiten.IsKeyPressed(ebiten.KeyS) || ebiten.IsKeyPressed(ebiten.KeyDown) {
		i.keys[ebiten.KeyS] = true
		i.keys[ebiten.KeyDown] = true
	}
	if ebiten.IsKeyPressed(ebiten.KeyA) || ebiten.IsKeyPressed(ebiten.KeyLeft) {
		i.keys[ebiten.KeyA] = true
		i.keys[ebiten.KeyLeft] = true
	}
	if ebiten.IsKeyPressed(ebiten.KeyD) || ebiten.IsKeyPressed(ebiten.KeyRight) {
		i.keys[ebiten.KeyD] = true
		i.keys[ebiten.KeyRight] = true
	}
}
